import pymysql

import re

from conn_mysql.conn_mysql import ConnectMysql

class ApiRegister(ConnectMysql):

    def verify_name(self, name):

        if not 1<len(name)<20:

            return False

        if str(name).isdigit():

            return False

        if str(name).isspace():
    
            return False

        if str(name).isalnum():
    
            return True

        RE_CHINESE = re.compile(r'^[\u4e00-\u9fa5]{1,8}$')

        if RE_CHINESE.findall(str(name)):
            
            return True

        elif str(name).isalpha():

            return True

        else:

            return False


    def is_phone(self, phone):
        phone_pat = re.compile('^(13\d|14[5|7]|15\d|166|17[3|6|7]|18\d)\d{8}$')
        res = re.search(phone_pat, phone)
        if not res:
            return False
        return True

    def company_register(self, company_name, linkman, phone, need, party):
    
        sql = '''INSERT INTO company
            (company_name, linkman, phone, need, party)
            values
            (%s,%s,%s,%s,%s);       
            '''

        result_info = self.insert_info(sql, (company_name, linkman, phone, need, party))

        return result_info

    def open_id(self, openid):
        '''查询openid表是否存在该openid并查出所对应的信息'''

        sql = 'select * from openid where openid = %s'

        result_info = self.select_one(sql, (openid))

        return result_info

    def openid_register(self, openid, company_id):

        sql = '''UPDATE openid SET company_id=%s WHERE openid=%s'''
        
        result_info = self.insert_info(sql, (company_id, openid))

        return result_info

    def need_his_register(self, company_id, need):
    
        sql = '''INSERT INTO need_his(company_id, need) values (%s,%s);'''
        
        result_info = self.insert_info(sql, (company_id, need))

        return result_info

    def register(self, methods, company_name, linkman, phone, need, party, openid):

        if not methods == 'POST':
            
                return {
                    'status': -1,
                    'errorCode': 4,
                    'errorMsg':f'请检查request.method:{methods}请重新请求',
                    'resultBody':None
                }

        else:

            if all((company_name, linkman, phone, need, party, openid)):

                if not self.verify_name(company_name):

                    return {
                        'status': -1,
                        'errorCode': 9,
                        'errorMsg':f'请检查参数是否合格,company_name:{company_name}请重新输入',
                        'resultBody':None
                    }

                if not self.verify_name(linkman):

                    return {
                        'status': -1,
                        'errorCode': 8,
                        'errorMsg':f'请检查参数是否合格,linkman:{linkman}请重新输入',
                        'resultBody':None
                    }

                phone_state = self.is_phone(phone)

                if not phone_state:

                    return {
                        'status': -1,
                        'errorCode': 2,
                        'errorMsg':f'请检查参数是否合格,phone:{phone}请重新输入',
                        'resultBody':None
                    }

                if not party in ('A','B'):
            
                    return {
                        'status': -1,
                        'errorCode': 3,
                        'errorMsg':f'请检查参数是否合格party:{party},A or B请重新输入',
                        'resultBody':None
                    }

                if not self.open_id(openid):
    
                    return {
                        'status': -1,
                        'errorCode': 5,
                        'errorMsg':f'请检查参数是否存在,openid:{openid}请重新输入',
                        'resultBody':None
                    }

                if self.open_id(openid):

                    company_id = self.open_id(openid)['company_id']

                    if company_id == None:

                        company_info = self.company_register(company_name, linkman, phone, need, party)

                        if company_info:

                            company_id = company_info

                            openid_work = self.openid_register(openid, company_id)

                            if openid_work:

                                if self.need_his_register(company_id, need):

                                    return {

                                            'status': 1,
                                            'errorCode': None,
                                            'errorMsg':None,
                                            'resultBody': {'company_id':company_id},
                                        }
                                
                                else:
                                    return {

                                            'status': -1,
                                            'errorCode':7 ,
                                            'errorMsg':'mysql未知error',
                                            'resultBody': None,
                                        }

                            else:

                                return {

                                    'status': -1,
                                    'errorCode': 7,
                                    'errorMsg':'mysql未知error',
                                    'resultBody': None,
                                } 

                        else:
    
                            return {

                                'status': -1,
                                'errorCode': 7,
                                'errorMsg':'mysql未知error',
                                'resultBody': None,
                            } 

                    else:

                        return {

                                'status': -1,
                                'errorCode': 7,
                                'errorMsg':'该账号已注册',
                                'resultBody': None,
                            } 

            else:

                return {

                    'status': -1,
                    'errorCode': 6,
                    'errorMsg':'请检查提交的数据是否有空值',
                    'resultBody': None,
                } 
